<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="author" content="wa">
    <title>自动轮播图</title>
    <style>
        ul,li{
            margin: 0;
            padding: 0;
            list-style-type: none;
        }
        #box{
            width: 520px;
            height: 280px;
            margin: 100px auto;
            position: relative;
        }
        #box .list li{
            position: absolute;
            top: 0;
            left: 0;
        }
        #box .list li.current{
            display: block;
        }
        #box .count{
            position: absolute;
            right: 10px;
            bottom: 10px;
        }
        #box .count li{
            float: left;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background-color: #fa0;
            text-align: center;
            line-height: 20px;
            margin-left: 10px;
            color: #fff;
            opacity: 0.8;
            cursor: pointer;
        }
        #box .count li.current{
            background-color: #f60;
            opacity: 1;
        }
    </style>
</head>
<body>
    <div id="box">
        <ul class="list">
            <li class="current"><img src="../img/01.jpg" alt=""></li>
            <li><img src="../img/02.jpg" alt=""></li>
            <li><img src="../img/03.jpg" alt=""></li>
            <li><img src="../img/04.jpg" alt=""></li>
            <li><img src="../img/05.jpg" alt=""></li>
        </ul>
        <ul class="count">
            <li class="current">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
        </ul>
    </div>
</body>
</html>
<script>

    var box=document.querySelector('#box');
    var aLi=document.querySelectorAll('.list li');
    var aNum=document.querySelectorAll('.count li');
    // 记录当前显示的图片的索引
    var current=0;
    // 保存定时器
    var timer=null;

    // 先写自动的
    timer=setInterval(autoPlay,1000);
    function autoPlay(){
        current ++;
        if(current>=aLi.length){
            current=0;
        }
        show(current);
    }

    // 手动的
    for(var i=0;i<aNum.length;i++){
        // 存储下标
        aNum[i].index=i;
        aNum[i].onmouseover=function(){
            show(this.index);
        }
    }

    // 处理图片的显示和隐藏
    function show(index){
        // 手动改变时，需要更新current的值
        current=index;
        // 当前图片显示时，那么其他的图片应该隐藏
        for(var i=0;i<aLi.length;i++){
            aLi[i].style.display='none';
            aNum[i].className='';
        }
        // 让当前的图片显示
        aLi[index].style.display='block';
        // 改变图片对应的数字的状态
        aNum[index].className='current';
    }

    // 当鼠标移入box容器的时候，让autoPlay停止，鼠标移出的继续自动播放
    box.onmouseover=function(){
        clearInterval(timer);
    }
    box.onmouseout=function(){
        timer=setInterval(autoPlay,1000);
    }

</script>